package com.cuz.hot100;

public class Q8strToInt {
    static class T{
        public int myAtoi(String s) {
            if(s==null){
                return 0;
            }
            s = s.trim();
            if(s.length()==0){
                return 0;
            }

            char[]chars = s.toCharArray();
            int result = 0;
            int index = 0;
            boolean neg = chars[index]=='-';
            if(neg) index++;

            int max = Integer.MAX_VALUE/10;
            int maxLast = Integer.MAX_VALUE%10;
            int min = Integer.MIN_VALUE/10;
            int minLast = Integer.MIN_VALUE%10;

            while(index<chars.length){

                char c = chars[index];
                int v = c - '0';
                if(v<0||v>9){
                    break;
                }
                if(neg){

                    if(-result < min){
                        return Integer.MIN_VALUE;
                    }
                    if(-result == min && -v<minLast){
                        return Integer.MIN_VALUE;
                    }
                }else{
                    if(result > max){
                        return Integer.MAX_VALUE;
                    }
                    if(result == max && v>maxLast){
                        return Integer.MAX_VALUE;
                    }
                }

                result = result * 10 + v;
                index ++;
            }

            return neg?-result:result;
        }
    }
    public static void main(String[] args) {

        System.out.println(new T().myAtoi("-91283472332"));
    }

    public static int myAtoi1(String s) {
        if (s == null || s.length() == 0) {
            return 0;
        }
        s = s.trim();
        if (s.length() == 0) {
            return 0;
        }
        char[] charArray = s.toCharArray();
        boolean isNeg = charArray[0] == '-';
        int end = (charArray[0] == '+' || charArray[0] == '-') ? 1 : 0;
        int res = 0;
        boolean hasNum = false;
        int dig = 0;
        boolean outBounds = false;
        for (int index = charArray.length - 1; index >= end; index--) {
            //当前字符是数字
            if (isNumChar(charArray[index])) {
                //没有数字 出现过
                if (!hasNum) {
                    hasNum = true;
                }
                //如果当前数字加上越界了
                long pow = (long) ((charArray[index] - '0') * Math.pow(10, dig));
                if (((long) res) + pow > Integer.MAX_VALUE ||((long) res) + pow <0) {
                    res = 0;
                    dig = 0;
                    outBounds = true;
                } else {
                    if (!outBounds) {
                        res += pow;
                    }
                }
                dig++;
            } else {
                if (hasNum) {
                    hasNum = false;
                    res = 0;
                    dig = 0;
                    outBounds = false;
                }
            }
        }
        if (outBounds) {
            if (isNeg) {
                return Integer.MIN_VALUE;
            } else {
                return Integer.MAX_VALUE;
            }
        }
        return isNeg ? -res : res;
    }

    public static boolean isNumChar(char c) {
        return c >= '0' && c <= '9';
    }
}
